import CryptoJS from 'crypto-js'

const key = CryptoJS.enc.Utf8.parse('123456')
// const iv = CryptoJS.enc.Utf8.parse('123456')

export default {
	//aes加密
	encrypt(word) {
		// console.log(CryptoJS.mode.ECB, CryptoJS.pad.Pkcs7)
		let encrypted = ''
		if (typeof word == 'string') {
			const srcs = CryptoJS.enc.Utf8.parse(word)
			encrypted = CryptoJS.AES.encrypt(srcs, key, {
				// iv: iv,
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			})
		} else if (typeof word == 'object') {
			//对象格式的转成json字符串
			const data = JSON.stringify(word)
			console.log(data)
			const srcs = CryptoJS.enc.Utf8.parse(data)
			encrypted = CryptoJS.AES.encrypt(srcs, key, {
				// iv: iv,
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			})
		}
		return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
	},
	// aes解密
	decrypt(word) {
		const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
		const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
		const decrypt = CryptoJS.AES.decrypt(srcs, key, {
			// iv: iv,
			mode: CryptoJS.mode.ECB,
			padding: CryptoJS.pad.Pkcs7
		})
		const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
		return decryptedStr.toString()
	}
}
